<?php
namespace app\index\controller;
use think\Controller;
/**
* 上传数据
*/
class Uploads extends Controller{
	// 下注请求andicap是盘口,bet是目录,bet是个别
	function uplaodszt($data){
		if(!$data){
			// 没有插入生成订单目录
			return 1;
		}
		$datatime = 0;
		$opent = model('openplate')->findall();
		foreach($opent as $key =>$o){
			if(time() >= $o['opentime'] &&time() <= $o['tclosetime'] ){
				$datatime = $o['datetime'];
			}
		}
		$palte = model('openplate')->findtype($datatime);
		// mp($palte);
		if($palte==0){
			// 投注失败
			return 8;
			exit();
		}

		$price = 0;
		$data = json_decode($data,true);
		if( is_array( $data ) ){
			foreach($data as $key =>$item){
				$price += (int)$item['price'];
				$data[$key]['uid'] = session('userid');
				if(!$item['rate']){
					return 8;
					exit();
				}
				if($item['bet']==2||$item['bet']==3||$item['bet']==4||$item['bet']==5){
					$palte = model('openplate')->findstatus($datatime);
					if($palte==0){
						// 投注失败
						return 8;
						exit();
					}
				}
			}
		}else{
			$data = json_decode($data,true);
			$price = $data['price'];
		}
		
		$tou = model("user")->findpot(session('userid'));
		if($tou==1){
			return 8;
			exit();
		}
		$zdtz = model('user')->where('id',session('userid'))->find();
		if($price>=$zdtz['zdtouzhu']){
			$result['user_id'] = session('userid');
			$result['times'] = time();
			$result['status'] = 1;
			$result['methods'] = 1;
			$result['datatime'] = $datatime;
			$result['price'] = $price;
			model('ordered')->insert($result);
			$order_id= db('ordered')->getLastInsID();
			if($order_id){
				$results = $data;
					$resuser = model('user')->findprice(session('userid'));
					$employ = $resuser['employ'];
					$employ += $price;
					if($resuser['moneys']<$employ){
						return 0;
						exit();
					}
				foreach ($results as $key => $v) {
					$results[$key]['order_id'] = $order_id;
					// $results[$key] = $this->array_remove($results[$key],"rate");
					// mp($employ);
					model('orders')->insert($results[$key]);

				}
				//下注股东得钱,用户-钱
				model('result')->share(session("userid"),$price);

				
				echo "success";
				exit();
			}else{
				// 没有插入生成订单目录
				return 1;
			}
		}else{
			// 订单价格不能少于2
			return 2;
		}
		
	}
	function isuplads($data){
		if(!$data){
			// 没有插入生成订单目录
			return 1;
		}
		$price = 0;
		$datatime = 0;
		$opent = model('openplate')->findall();
		foreach($opent as $key =>$o){
			if(time() >= $o['opentime'] &&time() <= $o['tclosetime'] ){
				$datatime = $o['datetime'];
			}
		}
		$palte = model('openplate')->findtype($datatime);
		// mp($palte);
		if($palte==0){
			// 投注失败
			return 8;
			exit();
		}
		$tou = model("user")->findpot(session('userid'));
		if($tou==1){
			return 8;
			exit();
		}
		$data = json_decode($data,true);
		$price = $data['price'];
		$zdtz = model('user')->where('id',session('userid'))->find();
		if($price>=$zdtz['zdtouzhu']){
			$result['user_id'] = session('userid');
			$result['times'] = time();
			$result['status'] = 1;
			$result['methods'] = 1;
			$result['datatime'] = $datatime;
			$result['price'] = $price;
			model('ordered')->insert($result);
			$order_id= db('ordered')->getLastInsID();
			if($order_id){
				$results = $data;
					$resuser = model('user')->findprice(session('userid'));
					$employ = $resuser['employ'];
					$employ += $price;
					if($resuser['moneys']<$employ){
						return 0;
						exit();
					}
					$results['uid'] = session('userid');
					$results['order_id'] = $order_id;
					// $results[$key] = $this->array_remove($results[$key],"rate");
					// mp($employ);
					model('orders')->insert($results);
					model('user')->where('id',session('userid'))->update(['employ'=>$employ]);
					echo "success";
					exit();
			}else{
				// 没有插入生成订单目录
				return 1;
			}
		}else{
			// 订单价格不能少于2
			return 2;
		}

	}
	function array_remove($data,$key){  
	    if(!array_key_exists($key, $data)){  
	        return $data;  
	    }  
	    $keys = array_keys($data);  
	    $index = array_search($key,$keys);  
	    if($index !== FALSE){  
	        array_splice($data, $index,1);  
	    }  
	    return $data;  
	}
 
}